<template>
	<view class="content">
		<scroll-view class="scroll-view" scroll-y="true" @scrolltolower="lower">
			<view class="item" v-for="(item, index) in list" :key="index">
				<view class="left">
					{{$t('pages/client/wallet/discount-coupon')}}
				</view>
				<view class="right">
					<view class="title">
						{{ item.coupon_title }}
					</view>
					<view class="bottom">
						<view class="time">
							{{$t('coupon.time')}}：{{item.start_time}}-{{item.expire_time}}
						</view>
						<view class="button" :class="item.is_used == 1?'hui':''" @click="jumpPage(item)">
							{{ item.is_used == 1?$t('haveBeenUsed'):$t('unused')}}</view>
					</view>
				</view>
			</view>
			<uni-load-more :status="status" />
		</scroll-view>
	</view>
</template>

<script>
	import utils from '@/utils/util.js'
	export default {
		data() {
			return {
				status: 'more',
				list: [],
				total: 0,
				params: {
					page: 1,
					page_size: 10,
					lang: uni.getStorageSync('language')
				},
				type: 0
			}
		},
		onLoad(options) {
			if (options.type) {
				this.type = options.type
			}
			this.myCoupon()
			utils.setNavTitle()
		},
		methods: {
			// 我的优惠券列表
			myCoupon() {
				this.params.token = uni.getStorageSync("userinfo").token;
				this.status = 'loading'
				uni.request({
					url: this.configs.webUrl + '/api/coupon/myCoupon',
					data: this.params,
					success: res => {
						console.log('myCoupon', res);
						if (res.data.code == 1) {
							this.list = this.list.concat(res.data.data.list)
							this.list.forEach(item => {
								this.$set(item, 'start_time', utils.formatDate(item.start_time * 1000))
								this.$set(item, 'expire_time', utils.formatDate(item.expire_time *
									1000))
							})
							this.total = res.data.data.count
							// this.status = 
							if (this.total > this.params.page * this.params.page_size) {
								this.status = 'more'
							} else {
								this.status = 'noMore'
							}
						}
					},
				});
			},
			lower() {
				if (this.total > this.params.page * this.params.page_size) {
					this.params.page++
					this.myCoupon()
				}
			},
			jumpPage(val) {
				if (val.is_used == 0) {
					if (this.type == 1) {
						let pages = getCurrentPages(); //获取所有页面栈实例列表
						let nowPage = pages[pages.length - 1]; //当前页页面实例
						let prevPage = pages[pages.length - 2]; //上一页页面实例
						prevPage.$vm.params.coupon_id = val.coupon_id; //修改上一页data里面的searchVal参数值为1211
						uni.navigateBack();
					}
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		padding: 20rpx;
	}

	.scroll-view {
		width: 100%;
		height: calc(100vh - 40rpx);

		.item {
			background: #fff;
			box-shadow: 0rpx 4rpx 8rpx 2rpx rgba(0, 0, 0, 0.09);
			border-radius: 20rpx;
			margin-bottom: 20rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			overflow: hidden;

			.left {
				display: flex;
				align-items: center;
				justify-content: center;
				width: 180rpx;
				height: 174rpx;
				font-size: 32rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 600;
				color: #FFFFFF;
				background: linear-gradient(314deg, #F75581 0%, #C860FF 100%);
			}

			.right {
				width: calc(100% - 180rpx);
				padding: 20rpx;

				.title {
					font-size: 28rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					color: #2F3438;
				}

				.bottom {
					margin-top: 20rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;

					.time {
						font-size: 20rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						color: #7B7B7B;
						width: 80%;
					}

					.button {
						font-size: 24rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						color: #FFFFFF;
						background: #F75581;
						border-radius: 20rpx;
						padding: 5rpx 10rpx;
					}

					.hui {
						color: #AAAAAA;
						background: #EAEAEA;
					}
				}
			}
		}
	}
</style>